package ferp.android.social;

import android.app.Activity;
import android.content.Intent;
import android.content.res.Resources;
import android.os.AsyncTask;
import android.view.View;
import android.widget.ImageView;
import com.facebook.Session;
import ferp.android.R;
import ferp.android.dialogs.ShareDialog;
import ferp.android.social.fb.FbConnector;
import ferp.android.social.ok.OkConnector;
import ferp.android.social.twvk.tw.TwConnector;
import ferp.android.social.twvk.vk.VkConnector;
import ferp.core.card.Card;
import ferp.core.game.Bid;
import ferp.core.game.Game;

/**
 * User: olegs
 * Date: 5/11/13 12:31 AM
 */
public abstract class Connector
{
  // login listener interface
  public interface LoginListener
  {
    void onLoginFinished(int requestCode, boolean loggedIn);
  }

  // post error data
  public static final class PostError
  {
    public long code;
    public String description;

    public PostError(long code, String description)
    {
      this.code = code;
      this.description = description;
    }
  }

  // post listener interface
  public interface PostListener
  {
    public void onPostCompleted(Connector c, PostError error);
  }

  // preference
  public static final String SOCIAL = "social";

  // request codes
  public static final class RequestCode
  {
    public static final int OK = 1000;
    public static final int VK = 1001;
    public static final int TW = 1002;
    public static final int FB = Session.DEFAULT_AUTHORIZE_ACTIVITY_CODE;
  }

  // connectors
  public static final OkConnector ok = new OkConnector();
  public static final VkConnector vk = new VkConnector();
  public static final FbConnector fb = new FbConnector();
  public static final TwConnector tw = new TwConnector();

  public static Post createPost(Activity owner, Game g, String message)
  {
    Post p;

    if (g.bid == null)
      p = createPassingPost(owner, g);
    else
      if (g.bid.isMisere())
        p = createMiserePost(owner, g);
      else
        p = createTrickingPost(owner, g);

    p.message = message.trim();

    return p;
  }

  // methods
  public void login(LoginListener loginListener, Activity owner, int requestCode)
  {
    this.loginListener = loginListener;

    doLogin(owner, requestCode);
  }

  public void post(PostListener postListener, Activity owner, Post post)
  {
    PostTask task = new PostTask(postListener, owner, post);

    task.execute();
  }

  public abstract int getRequestCode();
  public abstract boolean isLoggedIn(Activity owner);
  public abstract void onActivityResult(Activity owner, int result, Intent intent);
  public abstract void logout(Activity owner);

  // get corresponding social network's login icon
  public abstract ImageView getLogoutIcon(ShareDialog dialog);
  // get corresponding social network's icon for post progress animation
  public abstract ImageView getProgressIcon(ShareDialog dialog);
  // get corresponding social network's icon for post toast
  public abstract ImageView getPostToastIcon(View toast);
  // get corresponding social network's icon for logout toast
  public abstract ImageView getLogoutToastIcon(View toast);

  protected class PostTask extends AsyncTask<Void, Void, PostError>
  {
    protected PostTask(PostListener postListener, Activity owner, Post post)
    {
      this.postListener = postListener;
      this.owner = owner;
      this.post  = post;
    }

    @Override
    protected PostError doInBackground(Void... voids)
    {
      return publish(owner, post);
    }

    @Override
    protected void onPostExecute(PostError error)
    {
      super.onPostExecute(error);

      postListener.onPostCompleted(Connector.this, error);
    }

    private PostListener postListener;
    private Activity owner;
    private Post post;
  }

  protected PostError publish(Activity owner, Post post) {return null;}

  protected abstract void doLogin(Activity owner, int requestCode);

  protected LoginListener loginListener;

  private static Post createPassingPost(Activity owner, Game g)
  {
    Resources r = owner.getResources();
    int tricks = g.getTricks(g.human().id());
    Post p = new Post();

    p.result = tricks == 0 ? r.getString(R.string.post_passing_no_tricks) : String.format(r.getString(R.string.post_passing_tricks), tricks);

    return p;
  }

  private static Post createMiserePost(Activity owner, Game g)
  {
    Resources r = owner.getResources();
    int tricks = g.getTricks(g.human().id());
    Post p = new Post();

    if (g.declarer() == g.human())
      p.result = tricks == 0 ? r.getString(R.string.post_misere_no_tricks) : String.format(r.getString(R.string.post_misere_tricks), tricks);
    else
      p.result = r.getString(R.string.post_misere_failed_to_catch);

    return p;
  }

  private static Post createTrickingPost(Activity owner, Game g)
  {
    Resources r = owner.getResources();
    int tricks = g.getTricks(g.human().id());
    Bid contract = g.getDeclarerBid().current;
    Post p = new Post();

    if (g.declarer() == g.human())
    {
      String text;

      if(contract.isWithout3())
      {
        text = "" + contract.tricks + contract.trump;

        p.result = String.format(r.getString(R.string.post_tricking_without3), text);
      }
      else
      {
        text = contract.trump == Card.Suit.NONE ? "" + contract.tricks + " " + r.getString(R.string.no_trump_full) : contract.toString();

        p.result = String.format(r.getString(R.string.post_tricking_tricks), text, tricks);
      }
    }
    else
    {
      Bid bid = g.getBid(g.human().id()).current;

      if (bid.isWhist() || bid.isHalfWhist())
        p.result = String.format(r.getString(R.string.post_tricking_whists), contract, tricks);
      else
        p.result = String.format(r.getString(R.string.post_tricking_pass), contract);
    }

    return p;
  }
}
